<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="searchForm.id"
            placeholder="要查询的消息ID"
            clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getUserList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true" v-if="hasAuth('sys:sendmessage:save')" >新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="确认批量删除？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu" v-if="hasAuth('sys:sendmessage:delete')" >批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="release_uid"
          label="发布者用户ID"
          width="190">
      </el-table-column>
      <el-table-column
          prop="receive_uid"
          label="接收者用户ID"
          width="190">
      </el-table-column>
      <el-table-column
          prop="desc"
          label="互助信息"
          width="250">
      </el-table-column>

      <el-table-column
          prop="is_handled"
          label="接单状态"
          width="150"px>
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.is_handled === 1" type="success">已接单</el-tag>
          <el-tag size="small" v-else-if="scope.is_handled === 0" type="danger">未接单</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="is_finish"
          label="结单状态"
          width="150"px>
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.is_finish === 1" type="success">已结单</el-tag>
          <el-tag size="small" v-else-if="scope.is_finish === 0" type="danger">未结单</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="statu"
          label="审核状态"
          width="150"px>
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 0" type="success">未审核</el-tag>
          <el-tag size="small" v-else-if="scope.row.statu === 1" type="danger">审核正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.statu === 2" type="danger">审核不正常</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--          prop="created"-->
      <!--          width="200"-->
      <!--          label="创建时间"-->
      <!--      >-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--          prop="icon"-->
      <!--          width="260px"-->
      <!--          label="操作">-->

      <!--        <template slot-scope="scope">-->
      <!--          <el-button type="text" @click="sendHandle">发布</el-button>-->
      <!--&lt;!&ndash;          <el-button type="text" @click="roleHandle(scope.row.id)">发布</el-button>&ndash;&gt;-->
      <!--&lt;!&ndash;          <el-divider direction="vertical"></el-divider>&ndash;&gt;-->
      <!--&lt;!&ndash;          <el-button type="text" @click="roleHandle(scope.row.id)">分配角色</el-button>&ndash;&gt;-->
      <!--&lt;!&ndash;          <el-divider direction="vertical"></el-divider>&ndash;&gt;-->

      <!--          <el-button type="text" @click="repassHandle(scope.row.id, scope.row.username)">重置密码</el-button>-->
      <!--          <el-divider direction="vertical"></el-divider>-->

      <!--          <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>-->
      <!--          <el-divider direction="vertical"></el-divider>-->
      <!--          <el-button type="text" @click="deletingHandle(scope.row.id)">删除</el-button>-->

      <!--          &lt;!&ndash;          <template>&ndash;&gt;-->
      <!--          &lt;!&ndash;            <el-popconfirm title="确定是否删除吗？" @confirm="delHandle(scope.row.id)">&ndash;&gt;-->
      <!--          &lt;!&ndash;              <el-button type="text" slot="reference">删除</el-button>&ndash;&gt;-->
      <!--          &lt;!&ndash;            </el-popconfirm>&ndash;&gt;-->
      <!--          &lt;!&ndash;          </template>&ndash;&gt;-->

      <!--        </template>-->
      <!--      </el-table-column>-->

    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>


    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="	发布者用户ID" prop="receive_uid" label-width="120px">
          <el-input v-model="editForm.receive_uid" autocomplete="off"></el-input>
        </el-form-item>
        <!--        <el-form-item label="性别"  prop="sex" label-width="100px">-->
        <!--          &lt;!&ndash;          <el-input v-model="editForm.sex" autocomplete="off"></el-input>&ndash;&gt;-->
        <!--          <el-select v-model="editForm.sex" placeholder="请选择">-->
        <!--            <el-option-->
        <!--                v-for="item in options"-->
        <!--                :key="item.value"-->
        <!--                :label="item.label"-->
        <!--                :value="item.value">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="接收者用户ID"  prop="receive_uid" label-width="120px">
          <el-input v-model="editForm.receive_uid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="互助信息"  prop="desc" label-width="100px">
          <el-input v-model="editForm.desc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="接单状态"  prop="is_handled" label-width="100px">
          <el-radio-group v-model="editForm.is_handled">
            <el-radio :label="0">未接单</el-radio>
            <el-radio :label="1">已接单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="结单状态"  prop="is_finish" label-width="100px">
          <el-radio-group v-model="editForm.is_finish">
            <el-radio :label="0">未接单</el-radio>
            <el-radio :label="1">已接单</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="互助要求"  prop="major" label-width="100px">-->
        <!--          <el-input v-model="editForm.major" autocomplete="off"></el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="入学时间"  prop="sign_time" label-width="100px">-->
        <!--          <el-input v-model="editForm.sign_time" autocomplete="off"></el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="手机号"  prop="phone" label-width="100px">-->
        <!--          <el-input v-model="editForm.phone" autocomplete="off"></el-input>-->
        <!--        </el-form-item>-->

        <el-form-item label="审核状态"  prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label="0">未审核</el-radio>
            <el-radio :label="1">审核正常</el-radio>
            <el-radio :label="2">审核不正常</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Role",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,

      total: 0,
      size: 10,
      current: 1,

      dialogVisible: false,
      editForm: {

      },
      // options: [{
      //   value: '1',
      //   label: '男'
      // }, {
      //   value: '2',
      //   label: '女'
      // }, {
      //   value: '3',
      //   label: '保密'
      // }],
      //
      //
      // tableData: [],
//校验
      editFormRules: {
        release_uid: [
          {required: true, message: '请输入发布者用户ID', trigger: 'blur'}
        ],
        receive_uid: [
          {required: true, message: '请输入接受者用户ID', trigger: 'blur'}
        ],
        desc: [
          {required: true, message: '请输入互助信息', trigger: 'blur'}
        ],
        is_handled: [
          {required: true, message: '请选择接单状态', trigger: 'blur'}
        ],
        is_finish: [
          {required: true, message: '请选择结单状态', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择审核状态', trigger: 'blur'}
        ]
      },

      multipleSelection: [],

      roleDialogFormVisible: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      roleForm: {},
      roleTreeData:  [],
      treeCheckedKeys: [],
      checkStrictly: true

    }
  },
  created() {
    this.getUserList()

    this.$axios.get("/sys/role/list").then(res => {
      this.roleTreeData = res.data.data.records
    })
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      console.log("勾选")
      console.log(val)
      this.multipleSelection = val;

      this.delBtlStatu = val.length == 0
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getUserList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getUserList()
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },

    getUserList() {
      this.$axios.get("/sys/sendmessage/list", {
        params: {
          id: this.searchForm.id,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/sys/sendmessage/' + (this.editForm.id?'update' : 'save'), this.editForm)
              .then(res => {

                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose:() => {
                    this.getUserList()
                  }
                });

                this.dialogVisible = false
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(id) {
      this.$axios.get('/sys/user/info/' + id).then(res => {
        this.editForm = res.data.data

        this.dialogVisible = true
      })
    },
    //单个删除
    deletingHandle(id, release_uid) {

      this.$confirm('确认删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/sys/user/repass", id).then(res => {
          this.$message({
            showClose: true,
            message: '恭喜你，操作成功',
            type: 'success',
            onClose: () => {
            }
          });
        })
      })
    },
    //批量删除
    delHandle(id) {

      var ids = []

      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }

      console.log(ids)

      this.$axios.post("/sys/user/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose:() => {
            this.getUserList()
          }
        });
      })
    },

    // sendHandle (id) {
    //   this.$confirm('是否确定发布', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     // type: 'warning'
    //   }).then(() => {
    //     this.$message({
    //       type: 'success',
    //       message: '发布成功!'
    //     });
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消发布'
    //     });
    //   });
    // },
    submitRoleHandle(formName) {
      var roleIds = this.$refs.roleTree.getCheckedKeys()

      console.log(roleIds)

      this.$axios.post('/sys/user/role/' + this.roleForm.id, roleIds).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose:() => {
            this.getUserList()
          }
        });

        this.roleDialogFormVisible = false
      })
    },
    repassHandle(id, username) {

      this.$confirm('将重置用户【' + username + '】的密码, 是否继续?', '提示：初始密码为123456', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/sys/sendmessage/repass", id).then(res => {
          this.$message({
            showClose: true,
            message: '恭喜你，操作成功',
            type: 'success',
            onClose: () => {
            }
          });
        })
      })
    }
  }
}
</script>

<style scoped>

.el-pagination {
  float: right;
  margin-top: 22px;
}

</style>